package cn.toma.config.handler;

import cn.toma.common.exception.FfBootException;
import cn.toma.common.vo.StringPool;
import cn.toma.config.web.utli.FfBootResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Path;
import java.util.List;
import java.util.Set;

@Slf4j
@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler
{

	@ExceptionHandler(value = Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public FfBootResponse handleException(Exception e)
	{
		log.error("系统内部异常，异常信息：", e);
		return new FfBootResponse().message("系统内部异常");
	}


	@ExceptionHandler(value = FfBootException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public FfBootResponse handleParamsInvalidException(FfBootException e)
	{
		log.error("系统错误：{}", e.getMessage());
		return new FfBootResponse().message(e.getMessage());
	}

	/**
	 * 统一处理请求参数校验(实体对象传参)
	 *
	 * @param e BindException
	 * @return FebsResponse
	 */
	@ExceptionHandler(BindException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public FfBootResponse validExceptionHandler(BindException e)
	{
		StringBuilder message = new StringBuilder();
		List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
		for (FieldError error : fieldErrors) {
			message.append(error.getField()).append(error.getDefaultMessage()).append(StringPool.COMMA);
		}
		message = new StringBuilder(message.substring(0, message.length() - 1));
		return new FfBootResponse().message(message.toString());

	}

	/**
	 * 统一处理请求参数校验(普通传参)
	 *
	 * @param e ConstraintViolationException
	 * @return FebsResponse
	 */
	@ExceptionHandler(value = ConstraintViolationException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public FfBootResponse handleConstraintViolationException(ConstraintViolationException e)
	{
		StringBuilder message = new StringBuilder();
		Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
		for (ConstraintViolation<?> violation : violations) {
			Path path = violation.getPropertyPath();
			String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), StringPool.DOT);
			message.append(pathArr[1]).append(violation.getMessage()).append(StringPool.COMMA);
		}
		message = new StringBuilder(message.substring(0, message.length() - 1));
		return new FfBootResponse().message(message.toString());
	}


	@ExceptionHandler(value = UnauthorizedException.class)
	@ResponseStatus(HttpStatus.FORBIDDEN)
	public void handleUnauthorizedException(Exception e)
	{
		log.error("权限不足，{}", e.getMessage());
	}
}
